package com.ljfiction.book.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.MyView.ContainsEmojiEditText;
import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.ljfiction.book.tools.TimeTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class LeaveActivity extends BaseActivity {

    @BindView(R.id.leave_remainder_num)
    TextView leaveRemainderNum;
    @BindView(R.id.leave_used_num)
    TextView leaveUsedNum;
    @BindView(R.id.leave_num)
    SuperButton leaveNum;
    @BindView(R.id.leave_edittext)
    ContainsEmojiEditText leaveEdittext;
    private NoBackgLodDialog noBackgLodDialog;
    private int setleaveNum = 0, useLeaveNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave);
        ButterKnife.bind(this);
        leaveRemainderNum.setText(String.valueOf(UserManageBean.userBean.getLeaveCard()));
        intData();
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/getUserUseMonthRecord")

                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (noBackgLodDialog == null) {
                            noBackgLodDialog = new NoBackgLodDialog(LeaveActivity.this, true);

                        }
                        noBackgLodDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (noBackgLodDialog != null) {
                            noBackgLodDialog.dismis();
                        }
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.getInt("returnCode") == 200) {
                                useLeaveNum = jsonObject.getInt("data");
                                leaveUsedNum.setText(String.valueOf(useLeaveNum));
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @OnClick({R.id.back_leave, R.id.leave_num_add, R.id.leave_num_less, R.id.leave_took})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_leave:
                goBack();
                break;
            case R.id.leave_num_add:
                if ((setleaveNum + useLeaveNum) >= 7) {
                    showToast("每月最多使用7张请假卡!");
                    return;
                }
                if (setleaveNum < TimeTool.getremainderDay()) {
                    setleaveNum++;
                    leaveNum.setText(String.valueOf(setleaveNum));
                } else {
                    showToast("请假天数不能大于本月剩余天数！");
                }
                break;
            case R.id.leave_num_less:
                if (setleaveNum > 0) {
                    setleaveNum--;
                    leaveNum.setText(String.valueOf(setleaveNum));
                } else {
                    showToast("不可以再减了！");
                }
                break;
            case R.id.leave_took:
                if (!(leaveEdittext.getText().toString().replaceAll(" ","").length()>1&&leaveEdittext.getText().toString().replaceAll(" ","").length()<201)){
                    showToast("请假理由字数不符合规则");
                    return;
                }
                if (setleaveNum < 1) {
                    showToast("请假天数不能为0！");
                } else if ((setleaveNum + useLeaveNum) > 7){
                    showToast("每月最多使用7张请假卡!");

                }else if (setleaveNum <= TimeTool.getremainderDay()){
                    setLeave();
                }else {
                    showToast("请假天数不能大于本月剩余天数！");
                }

                break;
        }
    }

    private void setLeave() {
        OkHttpUtils.post(HttpManager.IP + "/addLeaverecord")

                .params("userId", String.valueOf(UserManageBean.userBean.getUserId()))
                .params("userName", UserManageBean.userBean.getPseudonym())
                .params("leaveDay", String.valueOf(setleaveNum))
                .params("leaveReason", leaveEdittext.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (noBackgLodDialog == null) {
                            noBackgLodDialog = new NoBackgLodDialog(LeaveActivity.this, true);
                        }
                        noBackgLodDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (noBackgLodDialog != null) {
                            noBackgLodDialog.dismis();
                        }
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.getInt("returnCode") == 200) {
                                UserManageBean.userBean.setLeaveCard(UserManageBean.userBean.getLeaveCard() - setleaveNum);
                                useLeaveNum += setleaveNum;
                                leaveUsedNum.setText(String.valueOf(useLeaveNum));
                                leaveRemainderNum.setText(String.valueOf(UserManageBean.userBean.getLeaveCard()));
                                UserManageBean.backgroundRiches(LeaveActivity.this);
                            }
                            showToast(jsonObject.getString("msg"));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
